package hn.sigit.view.output;

import hn.sigit.dao.hnd.cadastre.HND_ParcelDAO;
import hn.sigit.logic.general.GeneralHelper;
import hn.sigit.logic.general.ResourceBundleHelper;
import hn.sigit.model.commons.IParcel;
import hn.sigit.model.commons.IProperty;
import hn.sigit.model.commons.IRight;
import hn.sigit.model.hnd.administrative.HND_SpatialZoneInTransaction;
import hn.sigit.model.hnd.administrative.HND_MunicipalTransaction;
import hn.sigit.model.hnd.cadastre.HND_Parcel;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Name("cadastralRecordHelper")
@Scope(value=ScopeType.CONVERSATION)
@AutoCreate
public class CadastralRecordHelper extends OutputHelperBase implements Serializable {
	private static final long serialVersionUID = 1L;
	
	
	@In(create=true)
	private GeneralHelper generalHelper;
	
	@In(create=true)
	private ResourceBundleHelper resBundle;
	
	private HND_MunicipalTransaction transaction;
	
	private List<IParcel> requestedParcels;

	
	public HND_MunicipalTransaction getTransaction() {
		return transaction;
	}
	public void setTransaction(HND_MunicipalTransaction transaction) {
		this.transaction = transaction;
	}
	
	
	public String getTransactionCode() {
		if (generalHelper == null || transaction == null) return "";
		
		return String.format("%s - %s", generalHelper.getJurisdictionCode(), transaction.getPresentationId());
	}
	public String cadastralKey(IParcel parcel) {
		String ckey = parcel.getCadastralKey();
		return ckey != null && !ckey.trim().equals("") ? ckey : resBundle.loadMessage("txt.no_cadastral_key");
	}
	
	public String fiscalAppraisal(IParcel parcel) {
		BigDecimal bd = parcel.getFiscalAppraisal();
		return bd != null ? bd.toString() : resBundle.loadMessage("txt.no_value");
	}
	public String fiscalAppraisalWords(IParcel parcel) {
		BigDecimal bd = parcel.getFiscalAppraisal();
		return bd != null ? spanishConvertNumberToWords(bd, "lempira(s)", "con", "centavo(s)") : resBundle.loadMessage("txt.no_value");
	}
	public String documentedArea(IParcel parcel) {
		BigDecimal bd = parcel.getDocumentedArea();
		return bd != null ? bd.toString() : resBundle.loadMessage("txt.no_value");
	}

	public List<RRRData> rightsTextList(IProperty property) {
		List<RRRData> rrrDataList = new ArrayList<RRRData>();
		for (IRight right : property.getRights()) {
			rrrDataList.add(new RRRData(
					resBundle.loadMessage(right.getType().name()),
					right.getParty().getExtParty().getName(),
					right.getShare().toString()));
			
		}
		if (rrrDataList.size() > 0)
			rrrDataList.get(rrrDataList.size() - 1).setLast();
		return rrrDataList;
	}
	public List<RRRData> rightsTextList(IParcel parcel) {
		return rightsTextList(parcel.getProperty());
	}
	public String rightsTextStr(IProperty property) {
		StringBuilder sb = new StringBuilder();
		for (RRRData rd : rightsTextList(property)) {
			sb.append(String.format("\"%s\" a favor de %s con una participación de %s",
					rd.getRrrName(),
					rd.getPartyName(),
					rd.getShare() + (!rd.isLast() ? ", " : "")));
		}
		
		if (sb.length() == 0)
			sb.append(resBundle.loadMessage("txt.no_neighborhood"));
		
		return sb.toString();
	}
	public String rightsTextStr(IParcel parcel) {
		return rightsTextStr(parcel.getProperty());
	}
	
	public String neighborHood(IParcel parcel) {
		String nhood = parcel.getNeighborhood();
		return nhood != null && !nhood.trim().equals("") ? nhood : resBundle.loadMessage("txt.no_neighborhood");
	}
	
	
	public List<IParcel> getRequestedParcels() {
		if (requestedParcels == null) {
			requestedParcels = new ArrayList<IParcel>();
			if (transaction != null)
				for (HND_SpatialZoneInTransaction szit : transaction.getSpatialZoneInTransactions())
					if (szit.getNeighborLevel() == 0 && szit.getSpatialZone() instanceof IParcel) //level 0 is requested
						requestedParcels.add((IParcel) szit.getSpatialZone());
		}
		return requestedParcels;
	}
	
	public List<IParcel> neighborParcels(IParcel parcel) {
		List<IParcel> parcelList = new ArrayList<IParcel>();
		
		if (parcel instanceof HND_Parcel)
			for (IParcel p : HND_ParcelDAO.loadNeighborsByHNDParcel((HND_Parcel) parcel)) {
				parcelList.add(p);
			}
		
		return parcelList;
	}
	
	
	public static class RRRData {
		private String rrrName;
		private String partyName;
		private String share;
		private boolean last = false;
		
		public RRRData(String rrrName, String partyName, String share) {
			this.rrrName = rrrName;
			this.partyName = partyName;
			this.share = share;
		}

		public String getRrrName() {
			return rrrName;
		}
		public String getPartyName() {
			return partyName;
		}
		public String getShare() {
			return share;
		}
		public boolean isLast() {
			return last;
		}

		public void setLast() {
			this.last = true;
		}
	}
}
